<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>ndgrid</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : 24/03/2004</div>
    <p>
      <b>ndgrid</b> - arrays for multidimensional function evaluation on grid</p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>[X, Y] = ndgrid(x,y)</tt>
      </dd>
      <dd>
        <tt>[X, Y, Z] = ndgrid(x,y,z)</tt>
      </dd>
      <dd>
        <tt>[X, Y, Z, T] = ndgrid(x,y,z,t)</tt>
      </dd>
      <dd>
        <tt>[X1, X2, ..., Xm] = ndgrid(x1,x2,...,xm)</tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>x, y, z, ...</b>
        </tt>: vectors</li>
      <li>
        <tt>
          <b>X, Y, Z, ...</b>
        </tt>: matrices in case of 2 input arguments, or else hypermatrices</li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
    This is an utility routine useful to create arrays for function evaluation on 2, 3, ..., n 
    dimensional grids. For instance in 2d, a grid is defined by two vectors, <tt>
        <b>x</b>
      </tt> and
    <tt>
        <b> y</b>
      </tt> of length nx and ny, and you want to evaluate a function (says <em>f</em>) 
    on all the grid points, that is on all the points of coordinates  <em>(x(i),y(j))</em> 
    with  <em>i=1,..,nx</em> and <em>j=1,..,ny</em>. In this case, this function can
    compute the two matrices <tt>
        <b>X,Y</b>
      </tt> of size <em>nx x ny</em> such that :
    </p>
    <pre>
      X(i,j) = x(i)   for all i in [1,nx]
      Y(i,j) = y(j)       and j in [1,ny]
     </pre>
    <p>
    and the evaluation may be done with <tt>
        <b>Z=f(X,Y)</b>
      </tt> (at the condition that you have 
    coded <tt>
        <b>f</b>
      </tt> for evaluation on vector arguments, which is done (in general) by 
    using the element-wise operators <tt>
        <b>.*</b>
      </tt>, <tt>
        <b>./</b>
      </tt> and <tt>
        <b>.^</b>
      </tt>
    in place of <tt>
        <b>*</b>
      </tt>, <tt>
        <b>/</b>
      </tt> and <tt>
        <b>^</b>
      </tt>).
    </p>
    <p>
    In the 3d case, considering 3 vectors <tt>
        <b>x,y,z</b>
      </tt> of length nx, ny and nz,  <tt>
        <b>X,Y,Z</b>
      </tt>
    are 3 hypermatrices  of size <em>nx x ny x nz</em> such that :
    </p>
    <pre>
      X(i,j,k) = x(i)  
      Y(i,j,k) = y(j)   for all (i,j,k) in [1,nx]x[1,ny]x[1,nz]
      Z(i,j,k) = z(k)
     </pre>
    <p>
    In the general case of m input arguments <tt>
        <b>x1, x2, .., xm</b>
      </tt>, then
    the m output arguments <tt>
        <b>X1, X2, .., Xm</b>
      </tt> are hypermatrices  of size 
    <em>nx1 x nx2 x ... x nxm</em> and :
    </p>
    <pre>
    Xj(i1,i2,...,ij,...,im) = xj(ij)   
    for all (i1,i2,...,im) in [1,nx1]x[1,nx2]x...x[1,nxm]  
     </pre>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>
// create a simple 2d grid
nx = 40; ny = 40;
x = linspace(-1,1,nx);
y = linspace(-1,1,ny);
[X,Y] = ndgrid(x,y);
// compute a function on the grid and plot it
//deff("z=f(x,y)","z=128*x.^2 .*(1-x).^2 .*y.^2 .*(1-y).^2");
deff("z=f(x,y)","z=x.^2 + y.^3")
Z = f(X,Y);
xbasc()
plot3d(x,y,Z, flag=[2 6 4]); xselect()

// create a simple 3d grid
nx = 10; ny = 6; nz = 4;
x = linspace(0,2,nx);
y = linspace(0,1,ny);
z = linspace(0,0.5,nz);
[X,Y,Z] = ndgrid(x,y,z);
// try to display this 3d grid ...
XF=[]; YF=[]; ZF=[];
for k=1:nz
   [xf,yf,zf] = nf3d(X(:,:,k),Y(:,:,k),Z(:,:,k));
   XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf];
end
for j=1:ny
   [xf,yf,zf] = nf3d(matrix(X(:,j,:),[nx,nz]),...
                     matrix(Y(:,j,:),[nx,nz]),...
                     matrix(Z(:,j,:),[nx,nz]));
   XF = [XF xf]; YF = [YF yf]; ZF = [ZF zf];
end
xbasc()
plot3d(XF,YF,ZF, flag=[0 6 3], leg="X@Y@Z")
xtitle("A 3d grid !"); xselect()
 </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="kron.htm">
        <tt>
          <b>kron</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
    <h3>
      <font color="blue">Author</font>
    </h3>
    <p>
     B. Pincon
  </p>
  </body>
</html>
